﻿using NetDh.Lib;
using NetDh.Presentation.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace NetDh.WebApiAndMvc.Sample.Controllers
{
    public class HomeController : BaseMvcController
    {
        /// <summary>
        /// 首页，需要登录后才可查看。
        /// </summary>
        public ActionResult Index()
        {
            var userId = this.CurrentUser.UserId;//取当前用户id的示例
            return View();
        }

        public string TestString()
        {
            return "string test";
        }

        [MvcActionFilter(AllowAnonymous = true)]//可匿名访问(不做身份验证)
        [ActionName("test_a")]//Action别名，访问地址变为:/home/test_a
        public string TestAnoymous()
        {
            return "no need login";
        }

        [MvcActionFilter(AllowAnonymous = true)]
        public ActionResult TestHttpRequest()
        {
            //测试页面中的ajax请求是否自动带cookie，是否刷新cookie过期时间
            //-->所有请求都会带cookie，如果slidingExpiration设置为true，有请求则cookie过期时间就会刷新。
            return View();
        }
        public ActionResult TestOriJson()
        {
            //原来，改造前。前端得到的是{UserName:"name2"}，不支持改名
            return Json(new TestJsonResult() { UserName = "name1" }, JsonRequestBehavior.AllowGet);
        }
        public ActionResult TestNewJson()
        {
            //改造后。使用Newtonsoft.Json序列化，不需要再指定JsonRequestBehavior.AllowGet，前端得到的是{user_name:"name2"}，支持[JsonProperty]改名
            return NewJson(new TestJsonResult() { UserName = "name2" });
        }

        [HttpPost]
        public IEnumerable<string> TestPost()
        {
            //return Json("ok");
            return new string[] { "value1", "value2" };
        }

        [MvcActionFilter(AllowAnonymous = true)]
        [HttpPost]
        public string testMvcPostException()
        {
            //模拟ajax请求抛异常和异常捕获处理
            throw new Exception("test post exception");
        }

        [MvcActionFilter(AllowAnonymous = true)]
        public string testMvcActionException()
        {
            //模拟抛异常和异常捕获处理
            throw new Exception("test exception");
        }

        public string ShowLog(string logType)
        {
            var logs = MemoryLog.GetLogs(logType);
            var sb = new StringBuilder();
            foreach (var log in logs)
            {
                sb.AppendLine(JsonConvert.SerializeObject(log));
            }
            return sb.ToString();
        }

        #region 测试WebClientX设置的请求超时时间
        [MvcActionFilter(AllowAnonymous = true)]
        [HttpGet]
        [HttpPost]
        public ActionResult t2()
        {
            System.Threading.Thread.Sleep(3000);
            return Content("休息了3秒，未超时");
        }
        [MvcActionFilter(AllowAnonymous = true)]
        [HttpGet]
        [HttpPost]
        public ActionResult t3()
        {
            System.Threading.Thread.Sleep(7000);
            return Content("休息了7秒，超过5秒，超时");
        }
        [MvcActionFilter(AllowAnonymous = true)]
        [HttpGet]
        [HttpPost]
        public ActionResult t4()
        {
            //先往客户端发送一部分内容，然后延时一段比timeout更长的时间，模拟出网速很慢的样子，再把剩下的字符串发送完毕。
            System.Threading.Thread.Sleep(3000);//请求超时为5秒
            Response.Write("hello");
            Response.Flush();//3秒后先发送一次，调用方就不会TimeOut了。
            System.Threading.Thread.Sleep(8000);//模拟，因网络原因，返回的数据传输了8秒。比如下载文件..
            return Content(" world!");
        }
        #endregion

    }
}
